<template>
  <div class="address">

    <Head :title="title"></Head>

    <div class="addresslist">
      <van-swipe-cell v-for="item in list" :key="item.id">
        <div class="list">
          <div @click="goOrder(item)">
            <div class="name">{{ item.name }}</div>
            <div class="number">
              <div class="mobile"><div>{{ item.mobile }}</div>
               <div v-if="item.is_default===1" class="defaultAddress">默认
              </div>
              </div>
              <div class="address2">{{ item.address }}</div>
              <div class="detail">{{ item.address_detail }}</div>
            </div>
          </div>
          <div>
            <van-icon name="edit" class="edit" size="30px" @click="goEdit(item.id)" />
          </div>
        </div>
        <template #right>
          <van-button square type="danger" text="删除" class="del" @click="delAddress(item.id)" />
        </template>
      </van-swipe-cell>
    </div>

    <div class="fixbox">
      <div class="bulit" @click="add">+ 新建地址</div>
    </div>
  </div>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router'

import { ref } from 'vue'
import { getListAction, deleteAction } from '@/api/mine/index'
import Head from '@/components/title.vue'
import { showToast } from 'vant'
const title = ref('地址管理')
const route = useRoute()
const router = useRouter()
const list = ref([])
// const defaultAddress = ref()
// 获取地址列表
getListAction({
  openId: localStorage.getItem('openId')
}).then(res => {
  console.log(res)
  list.value = res.data
  console.log(route)
  // defaultAddress.value = res.data.is_default
})
const goEdit = (id) => {
  router.push({ name: 'editView', query: { id } })
}
const add = () => {
  router.push({ name: 'editView' })
}
const goOrder = (val) => {
  localStorage.setItem('addressInfo', JSON.stringify(val))
  history.back()
}
// 删除地址
const delAddress = (id) => {
  deleteAction({ id }).then(res => {
    console.log(res)
    getListAction({
      openId: localStorage.getItem('openId')
    }).then(res => {
      console.log(res)
      list.value = res.data
      showToast('删除成功')
    })
  })
}
</script>

<script>
export default {
  name: 'addressView'
}
</script>

<style lang="scss" scoped>
.address {
  background-color: rgb(242, 242, 242);
  height: 2000px;

  .addresslist {
    width: 710px;
    background-color: #fff;
    margin: 90px auto 158px;

    .del {
      height: 100%;
    }

    .list {
      height: 228px;
      border: 1px solid rgba(223, 223, 223, 1);
      position: relative;

      .name {
        font-size: 30px;
        color: #333333;
        letter-spacing: 0;
        font-weight: 600;
        margin: 49px 21px 0px 34px;
      }

      .number {
        font-size: 28px;
        color: #999999;
        letter-spacing: 0;
        font-weight: 400;
        position: relative;
        left: 200px;
        top: -10px;
        width: 370px;
        // overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        .mobile {
          position: relative;
          top: -20px;
          display: flex;
          .defaultAddress{
            border: 1px solid #860c0c;
            padding: 5px;
            margin-left: 10px;
            color: #860c0c;
          }
        }
      }

      .edit {
        position: absolute;
        right: 51px;
        top: 90px;
        z-index: 9999;
      }
    }
  }

  .fixbox {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 190px;
    background-color: #fff;

    .bulit {
      position: fixed;
      bottom: 68px;
      left: 21px;
      width: 710px;
      height: 90px;
      background: #cea62a;
      border-radius: 44.5px;
      font-size: 30px;
      color: #ffffff;
      letter-spacing: 0;
      font-weight: 500;
      text-align: center;
      line-height: 90px;
    }
  }
}</style>
